package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "房间信息管理")
@RestController
@RequestMapping("/admin/room")
public class RoomController {

    @Autowired
    private RoomInfoService roomInfoService ;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService ;

    @Autowired
    private RoomFacilityService roomFacilityService ;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Operation(summary = "保存或更新房间信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
        boolean flag = roomSubmitVo.getId()!=null;

        if(flag){
            //删除旧的关系

            //1)删除旧的支付关系
            roomPaymentTypeService.remove(
                    new LambdaQueryWrapper<RoomPaymentType>()
                            .eq(RoomPaymentType::getRoomId,roomSubmitVo.getId()));

            //2)删除旧的标签关系
            roomLabelService.remove(
                    new LambdaQueryWrapper<RoomLabel>()
                            .eq(RoomLabel::getRoomId,roomSubmitVo.getId()));

            //3)删除旧的配套关系
            roomFacilityService.remove(
                    new LambdaQueryWrapper<RoomFacility>()
                            .eq(RoomFacility::getRoomId,roomSubmitVo.getId()));

            //4)删除旧的属性关系
            roomAttrValueService.remove(
                    new LambdaQueryWrapper<RoomAttrValue>()
                            .eq(RoomAttrValue::getRoomId,roomSubmitVo.getId()));

            //5)删除旧的租期关系
            roomLeaseTermService.remove(
                    new LambdaQueryWrapper<RoomLeaseTerm>()
                            .eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId())
            );

            //6)删除旧的图片
        }

        //新增
        roomInfoService.saveOrUpdate(roomSubmitVo);

        //保存支付方式
        roomPaymentTypeService.saveBatch(roomSubmitVo.getPaymentTypeIds().stream().map(paymentTypeId->
            RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build()
        ).collect(Collectors.toList()));

        //保存租期方式
        roomLeaseTermService.saveBatch(roomSubmitVo.getLeaseTermIds().stream().map(leaseTermId->
                RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build()
        ).collect(Collectors.toList()));

        //保存配套
        roomFacilityService.saveBatch(roomSubmitVo.getFacilityInfoIds().stream().map(facilityId->
                RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityId).build()
        ).collect(Collectors.toList()));

        //保存标签
        roomLabelService.saveBatch(roomSubmitVo.getLabelInfoIds().stream().map(labelId->
                RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelId).build()
        ).collect(Collectors.toList()));

        //保存属性
        roomAttrValueService.saveBatch(roomSubmitVo.getAttrValueIds().stream().map(attrValueId->
                RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build()
        ).collect(Collectors.toList()));


        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询房间列表")
    @GetMapping("pageItem")
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current, @RequestParam long size, RoomQueryVo queryVo) {
        IPage<RoomItemVo> page = Page.of(current,size);
        IPage<RoomItemVo> roomItemVoIPage = roomInfoService.pageItem(page,queryVo);
        return Result.ok(roomItemVoIPage);
    }

    @Operation(summary = "根据id获取房间详细信息")
    @GetMapping("getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
        return Result.ok(roomInfoService.getDetailById(id));
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        roomInfoService.removeRoomById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改房间发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
        roomInfoService.update(new LambdaUpdateWrapper<RoomInfo>().set(RoomInfo::getIsRelease,status).eq(RoomInfo::getId,id));
        return Result.ok();
    }

    @GetMapping("listBasicByApartmentId")
    @Operation(summary = "根据公寓id查询房间列表")
    public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
        return Result.ok(roomInfoService.list(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId,id)));
    }

}


















